package com.noob.assessment.api;

import lombok.Data;

/**
 * @author: zhengwq
 * datetime: 2020-04-18 15:17
 * description: api响应
 */
@Data
public class ApiResponse<T> {

    /**
     * 请求成功
     */
    private static final String SUCCESS = "200";

    /**
     * 请求异常
     */
    private static final String ERROR = "500";

    /**
     * 响应码
     */
    private String code;

    /**
     * 错误信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public static <T> ApiResponse<T> ok(T data) {
        ApiResponse<T> resp = new ApiResponse<>();
        resp.setCode(SUCCESS);
        resp.setData(data);
        return resp;
    }

    public static <T> ApiResponse<T> error(String message) {
        ApiResponse<T> resp = new ApiResponse<>();
        resp.setCode(ERROR);
        resp.setMsg(message);
        return resp;
    }

    public static <T> ApiResponse<T> error(String message, T data) {
        ApiResponse<T> resp = new ApiResponse<>();
        resp.setCode(ERROR);
        resp.setMsg(message);
        resp.setData(data);
        return resp;
    }

}

